//
//  NewHomeGamePlatformCell.swift
//  d11
//
//  Created by peale on 2018/11/19.
//  Copyright © 2018年 Tech_001. All rights reserved.
//

import UIKit
typealias PageSelectBlock = (_ dict: [String:Any]?) -> Void
class NewHomeGamePlatformCell: UITableViewCell {
    var pageSelectBlock: PageSelectBlock?
    func callClickBlock(_ block: @escaping PageSelectBlock) {
        pageSelectBlock = block
    }
    var platformView: PlatformView?
    var newPlatformView: NewPlatformView?
    var  homeGameView : HomeGameView?
    var selectIndex = 0
    var selectDict : [String:Any]?
    var isReloadPlatform : Bool = false
    var platfromW :CGFloat?
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    func setUpUI() -> Void {
        self.backgroundColor  = HexRGBAlpha(0xf4f7f9, 1)
        self.selectionStyle = UITableViewCell.SelectionStyle.none
        self.platfromW = 97.0
        if kIsIpad  == true{
            self.platfromW = 280
          self.newPlatformView =  NewPlatformView.init(list: [], frame: CGRect(x: 10, y: 0, width: 280, height: 417))
            self.newPlatformView?.callClickBlock({ (dict, row) in
                DispatchQueue.main.async {
                    self.selectIndex = row
                    if let title =  GlobalInfo.platformInfoData?[self.selectIndex]{
                        let string = title["f_name"] ?? ""
                        ToolHelper.statisticsPage(pageName:"首页-\(string)-\(string)按钮")
                    }
                    let dict = GlobalInfo.platformInfoData?[self.selectIndex]
                    let gameList = dict?["game_list"] as? [[String:Any]]
                    let gameCount = gameList?.count ?? 0
                    self.homeGameView?.starAnimationWithRow(row: self.selectIndex, gameCount: gameCount)
                    // self.homeGameView?.reload(row: self.selectIndex,isWanBo: isWanBo)
                    
                }
            })
            self.contentView.addSubview( self.newPlatformView!)
        }else{
             self.platfromW = 97.0
            self.platformView = PlatformView.init(list: [], frame: CGRect(x: 10, y: 0, width: 87, height: 417))
            self.platformView?.callClickBlock({ (row) in
                DispatchQueue.main.async {
                    self.selectIndex = row
                    if let title =  GlobalInfo.platformInfoData?[self.selectIndex]{
                        let string = title["f_name"] ?? ""
                        ToolHelper.statisticsPage(pageName:"首页-\(string)-\(string)按钮")
                    }
                    let dict = GlobalInfo.platformInfoData?[self.selectIndex]
                    let gameList = dict?["game_list"] as? [[String:Any]]
                    let gameCount = gameList?.count ?? 0
                    self.homeGameView?.starAnimationWithRow(row: self.selectIndex, gameCount: gameCount)
                    // self.homeGameView?.reload(row: self.selectIndex,isWanBo: isWanBo)
                    
                }
            })
            self.contentView.addSubview( self.platformView!)
        }
        
       
        if GlobalInfo.platformInfoData?.count ?? 0 > 0 {
            if let title =  GlobalInfo.platformInfoData?[0]{
                let string = title["f_name"] ?? ""
                ToolHelper.statisticsPage(pageName:"首页-\(string)-\(string)按钮")
            }
        }
        
        self.homeGameView = HomeGameView.init(list: GlobalInfo.platformInfoData?[selectIndex]["game_list"] as? [[String : Any]], frame:  CGRect(x: self.platfromW ?? 0, y: 3, width:kScreenWidth-(self.platfromW ?? 0) - 10 , height: 412))
        self.homeGameView?.callClickBlock({ (dict) in
            DispatchQueue.main.async {
                if self.pageSelectBlock != nil{
                 // let gamelist = GlobalInfo.platformInfoData?[selectIndex]["game_list"] as? [[String : Any]],
                    self.pageSelectBlock!(dict)
                }
            }
            
        })
         self.contentView.addSubview( self.homeGameView!)
    }
    func reloadDatas() -> Void {
        if self.isReloadPlatform == false {
            if kIsIpad == true{
                 self.newPlatformView?.reload()
            }else{
                 self.platformView?.reload()
            }
            
        }
        self.isReloadPlatform = true
        let dict = GlobalInfo.platformInfoData?[self.selectIndex]
        let gameList = dict?["game_list"] as? [[String:Any]]
        let gameCount = gameList?.count ?? 0
        self.homeGameView?.reload(row: self.selectIndex,gameCount: gameCount)
    }
}
